using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using ALib;
using ALib.Data.CSVOperation;
using ALib.Data.IO;
using TradingPlatform;
using TradingPlatform.MarketTiming;

namespace TradingPlatform.MarketTiming
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Only use this to update list.
            FormateRawTime();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MarketTimingGui());
        }

        private static void FormateRawTime()
        {
            List<string[]> tickerInfoRecordList = CSVEditor.Open(@"NewTickerInfo.csv");
            List<string> outputList = new List<string>();
            foreach (string[] tickerInfoRecord in tickerInfoRecordList)
            {
                int tradingPlatformTimeZoneIndex = int.Parse(tickerInfoRecord[(int) TickerInfoItemType.tradingPlatformTimeZoneIndex]);
                int exchangeTimeZoneIndex = int.Parse(tickerInfoRecord[(int)TickerInfoItemType.exchangeTimeZoneIndex]);
                string strategySymbol = string.Format(@"{0}@{1}", tickerInfoRecord[(int)TickerInfoItemType.strategySymbol], tickerInfoRecord[(int)TickerInfoItemType.exchange]);
                string exchangeTradingHours = tickerInfoRecord[(int)TickerInfoItemType.tradingHours];
                UtcConverter utcConverter = new UtcConverter(tradingPlatformTimeZoneIndex, exchangeTimeZoneIndex);
                string tradingPlatformTradingHours = ConvertTradingSessionString_TradingPlatformToExchange(exchangeTradingHours, utcConverter);
                string outputString = strategySymbol + "," + tradingPlatformTradingHours;
                if (!outputList.Contains(outputString))
                {
                    outputList.Add(outputString);
                }
            }
            FilesIO.ToOneFile(outputList, new FileInfo(@"MarketTimingConfiguration.csv"));
        }

        private static string ConvertTradingSessionString_TradingPlatformToExchange(string exchangeTradingHours, UtcConverter utcConverter)
        {
            string[] times = exchangeTradingHours.Split(new char[] { '&', '-' });

            string tradingPlatformTradingHours = string.Empty;
            for (int i = 0; i < times.Length; i++)
            {
                TimeSpan exchangeTime = DateTimeConverter.ToTime(times[i]);
                DateTime exchangeDateTime = (DateTime.Now.Date + exchangeTime);
                string tradingPlatformTimeString = utcConverter.ExchangeTimeToTradingPlatformTime(exchangeDateTime).ToString("HH:mm:ss");
                if (i == 0)
                {
                    tradingPlatformTradingHours += tradingPlatformTimeString;
                }
                else
                {
                    if (i % 2 == 1)
                    {
                        tradingPlatformTradingHours += "-" + tradingPlatformTimeString;
                    }
                    else
                    {
                        tradingPlatformTradingHours += "&" + tradingPlatformTimeString;
                    }
                }
            }
            return tradingPlatformTradingHours;
        }
    }
}